package com.blog.controller;

import com.blog.entity.Section;
import com.blog.service.SectionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
@Slf4j
public class SectionsController {

    @Autowired
    private SectionService sectionService;


    @GetMapping("/sections")
    public ResponseEntity<?> getAllSections() {
        List<Section> sections = sectionService.getAllSections();
        return ResponseEntity.ok(sections);
    }

    /**
     * 添加
     * @param section
     * @return
     */
    @PostMapping("/sections/save")
    public ResponseEntity<?> saveSection(@RequestBody Section section) {
        sectionService.saveSection(section);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改
     *
     * @param section
     * @param request
     * @return
     */
    @PostMapping("/sections/update")
    public ResponseEntity<?> updateSection(@RequestBody Section section) {
        sectionService.updateSection(section);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/sections/delete/{id}") // 确保路径变量名与前端传递的参数名一致
    public ResponseEntity<?> deleteSection(@PathVariable Long id) {
        boolean deleted = sectionService.deleteSection(id);
        if (deleted) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }

}
